package aries.security;

/******************************************************************************
 * You may modify, publish, and use Aries for your own purpose. BUT
 * DON'T REMOVE THIS TEXT.
 * Visit my facebook: http://www.facebook.com/freeskies or
 * Follow my twitter: @freeskys
 *
 *
 * Author: Harditya Rahmat Ramadhan
 * Licence: GPLv2
 *****************************************************************************/

import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.swing.JComboBox;
import javax.swing.JPasswordField;
import javax.swing.JTextArea;
import javax.swing.JTextField;

public class Validation {
    
    /**
     * Check if a field is empty or not.
     * 
     * @param field
     * @param defaultValue
     * @return Boolean
     */
    public static Boolean isEmpty(JTextField field, String defaultValue) {
        return (field.getText().length() < 1 || field.getText().equals(defaultValue));
    }
    
    /**
     * Check if a field is empty or not.
     * 
     * @param field
     * @param defaultValue
     * @return Boolean
     */
    public static Boolean isEmpty(JTextArea field, String defaultValue) {
        return (field.getText().length() < 1 || field.getText().equals(defaultValue));
    }
    
    /**
     * Check if a combobox is empty or not.
     * 
     * @param comboBox
     * @param defaultIndex
     * @return Boolean
     */
    public static Boolean isEmpty(JComboBox comboBox, int defaultIndex) {
        return (comboBox.getSelectedIndex() == defaultIndex);
    }
    
    /**
     * Check if a field reach the maxlength.
     * 
     * @param field
     * @param maxlength
     * @return Boolean
     */
    public static Boolean reachMaxlength(JTextField field, int maxlength) {
        return (field.getText().length() > maxlength);
    }
    
    /**
     * Check if a field reach the maxlength.
     * 
     * @param field
     * @param maxlength
     * @return Boolean
     */
    public static Boolean reachMaxlength(JPasswordField field, int maxlength) {
        return (field.getText().length() > maxlength);
    }
    
    /**
     * Check if a field meet the minimum characters.
     * 
     * @param field
     * @param minimum
     * @return Boolean
     */
    public static Boolean meetMinimum(JTextField field, int minimum) {
        return (field.getText().length() >= minimum);
    }
    
    /**
     * Check if a field meet the minimum characters.
     * 
     * @param field
     * @param minimum
     * @return Boolean
     */
    public static Boolean meetMinimum(JPasswordField field, int minimum) {
        return (field.getText().length() >= minimum);
    }
    
    /**
     * Check if a field got a numeric value.
     * 
     * @param field
     * @return Boolean
     */
    public static Boolean isNumber(String text) {
        return isValid(text, "^[0-9]$");
    }
    
    /**
     * Check if a field contain valid email address.
     * <code>nama@domain.com</code>
     * 
     * @param text
     * @return Boolean
     */
    public static Boolean isEmailAddress(String text) {
        return isValid(text, ".+@.+\\.[a-z]+");
    }
    
    /**
     * Check if a field contain valid username.
     * Valid username is contain a-z, 0-9, - and _.
     * 
     * @param text
     * @return Boolean
     */
    public static Boolean isUsername(String text) {
        return isValid(text, "^[a-z0-9_-]$");
    }
    
    /**
     * Check if a field contain valid hexadecimal.
     * Example of valid hexadecimal: #000000.
     * 
     * @param text
     * @return Boolean
     */
    public static Boolean isHexadecimal(String text) {
        return isValid(text, "^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$");
    }
    
    /**
     * Check if a field contain valid IP Address.
     * Example of valid IP: 192.168.0.1.
     * 
     * @param text
     * @return Boolean
     */
    public static Boolean isIpAddress(String text) {
        return isValid(text, "^([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\.([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\.([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\.([01]?\\d\\d?|2[0-4]\\d|25[0-5])$");
    }
    
    /**
     * Check if a field only contain alphanumeric.
     * 
     * @param text
     * @return Boolean
     */
    public static Boolean isAlphaNumeric(String text) {
        return isValid(text, "[^a-zA-Z0-9 _.-]");
    }
    
    /**
     * Check if a text is valid with the pattern.
     * 
     * @param text
     * @param pattern
     * @return Boolean
     */
    public static Boolean isValid(String text, String pattern) {
        Pattern thePattern = Pattern.compile(pattern);
        Matcher matcher = thePattern.matcher(text);
        return matcher.matches();
    }
    
}
